﻿using DH.MQ;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestSendMessage
{
    public class Message : BaseMessage
    {
        public string MessageValue { get; set; }
        public override BaseMessage GetMessageResult(string bm)
        {
            BaseMessage result = Newtonsoft.Json.JsonConvert.DeserializeObject<Message>(bm);
            return result;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //RMQProvider provider = new RMQProvider();
            //Message bm = new Message();
            //Message bm2 = new Message();
            //Message bm3 = new Message();
            //Message bm4 = new Message();

            //Message bm5 = new Message();
            //Message bm6 = new Message();
            //Message bm7 = new Message();

            //Message bm8 = new Message();
            //Message bm9 = new Message();

            //var message = "发送给order " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            //var message2 = "发送给product的即时消息" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            //var message3= "发送给product的延时1分钟消息" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            //var message4 = "发送给product的延时3分钟消息" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            //var message5 = "发送给Test的即时消息" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            //var message6 = "发送给Test的延时1分钟消息" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            //var message7 = "发送给Test的延时2分钟消息" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            //var message8= "我是一个过了三分钟再发出来的即时消息" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            //var message9 = "我是一个过了三分钟再发出来的延迟2分钟消息" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            //bm.MessageValue = message;
            //bm2.MessageValue = message2;
            //bm3.MessageValue = message3;
            //bm4.MessageValue = message4;

            //bm5.MessageValue = message5;
            //bm6.MessageValue = message6;
            //bm7.MessageValue = message7;

            //bm8.MessageValue = message8;
            //bm9.MessageValue = message9;


            //provider.SendMessage(bm.BuildMessage(bm), "lu9.order.1234", false);

            //provider.SendMessage(bm2.BuildMessage(bm2), "lu9.product.12345", false);
            //provider.SendMessage(bm3.BuildMessage(bm3), "lu9.product.7894", true, 60);
            //provider.SendMessage(bm4.BuildMessage(bm4), "lu9.product.158", true, 180);

            //provider.SendMessage(bm5.BuildMessage(bm5), "lu9.Test.12345", false);
            //provider.SendMessage(bm6.BuildMessage(bm6), "lu9.Test.7894", true, 60);
            //provider.SendMessage(bm7.BuildMessage(bm7), "lu9.Test.158", true, 120);

            //System.Threading.Thread.Sleep(180000);
            //provider.SendMessage(bm8.BuildMessage(bm8), "lu9.Test.158",false);
            //provider.SendMessage(bm8.BuildMessage(bm9), "lu9.Test.158", true,120);


            //---------------------------------------------------模拟分布式事务问题---------------------------------------------------//
            //创建一个订单对象
            Order order = new Order();
            order.osn = "SN201607200003525";
            order.uid = 27;
            order.Money = 27.8M;
            order.PorductList.Add(new Product {ProductID = 300 });
            order.PorductList.Add(new Product { ProductID = 320 });
            order.PorductList.Add(new Product { ProductID = 782 });
            order.PorductList.Add(new Product { ProductID = 749 });
            
            RMQProvider provider = new RMQProvider();
            var message = Newtonsoft.Json.JsonConvert.SerializeObject(order);
            Message bm = new Message();
            bm.MessageValue = message;
            bm.IsNeedAck = true;//此消息需确认后发送
            
            string messageID = "";

            provider.SendMessage(bm.BuildMessage(bm, out messageID), "lu9.order.1234", false);


            System.Threading.Thread.Sleep(5000);
            AckMessage ackmessage = new AckMessage();
            ackmessage.MessageID = messageID;
            ackmessage.Operation = AckOperation.确认;
            ackmessage.SetTimeStamp();
            provider.SendMessage(Newtonsoft.Json.JsonConvert.SerializeObject(ackmessage), "lu9.order.1234",false);


        }
    }
    class Order
    {
        /// <summary>
        /// 订单编号
        /// </summary>
        public string osn { get; set; }
        /// <summary>
        /// 用户ID
        /// </summary>
        public int uid { get; set; }
        /// <summary>
        /// 订单金额
        /// </summary>
        public decimal Money { get; set; }

        public List<Product> PorductList = new List<Product>();
    }
    class Product
    {
        /// <summary>
        /// 商品编号
        /// </summary>
        public int ProductID { get; set; }
    }
    class Confirm
    {

    }
}
